import errorHandler from "errorhandler";

import app from "./app";

import * as config from "../config";

/**
 * Error Handler. Provides full stack - remove for production
 */
app.use(errorHandler());

const proxy = require("http-proxy-middleware");

var wsProxy = proxy(config.ws, {
  // pathRewrite: {
  //  '^/websocket' : '/socket',        // rewrite path.
  //  '^/removepath' : ''               // remove path.
  // },
  changeOrigin: true, // for vhosted sites, changes host header to match to target's host
  ws: true, // enable websocket proxy
  logLevel: "debug"
});

app.use(wsProxy); // add the proxy to express

/**
 * Start Express server.
 */
const server = app.listen(app.get("port"), "0.0.0.0", () => {
  console.log(
    "  App is running at http://localhost:%d in %s mode",
    app.get("port"),
    app.get("env")
  );
  console.log("  Press CTRL-C to stop\n");
});

// server.on("upgrade", wsProxy.upgrade);
server.on("upgrade", wsProxy.upgrade);
export default server;
